package com.example.multitenant.multitenant.config

import com.example.multitenant.base.Log
import org.springframework.stereotype.Component
import org.springframework.ui.ModelMap
import org.springframework.web.context.request.WebRequest
import org.springframework.web.context.request.WebRequestInterceptor
/**
 * @author xiang
 * @Date 2022/8/23 11:55
 **/

@Component
class TenantInterceptor : WebRequestInterceptor {

    companion object{
        private const val TAG = "TenantInterceptor"
    }

    override fun preHandle(request: WebRequest) {
        val tenantId = request.getHeader("X-TENANT-ID") ?: ""
        Log.i(TAG, "preHandle: tenantId: $tenantId")
        TenantContext.setTenantId(tenantId)
    }

    override fun postHandle(request: WebRequest, model: ModelMap?) {
        TenantContext.clear()
    }

    override fun afterCompletion(request: WebRequest, ex: Exception?) {
    }
}